package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.RabbitService;
import com.atguigu.gmall.constant.MqConst;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //  根据订单Id 和 支付方式查询数据. 联合主键：一张表的主键有多个字段！
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderId,orderInfo.getId()).eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(paymentInfoLambdaQueryWrapper);
        if (paymentInfoQuery!=null){
            return;
        }
        //  创建一个paymentInfo 对象
        PaymentInfo paymentInfo = new PaymentInfo();
        //  给对象赋值.
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        //  保存数据.
        paymentInfoMapper.insert(paymentInfo);
    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        //  调用mapper 层.
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo).eq(PaymentInfo::getPaymentType,paymentType);
        return paymentInfoMapper.selectOne(paymentInfoLambdaQueryWrapper);
    }

    @Override
    public void updatePaymentInfoStatus(String outTradeNo, String paymentType, Map<String, String> paramsMap) {
        try {
            //  如果订单已支付，则不需要在修改交易记录状态.
            PaymentInfo paymentInfoQuery = getPaymentInfo(outTradeNo, paymentType);
            if ("PAID".equals(paymentInfoQuery.getPaymentStatus())){
                return;
            }
            //  创建对象.
            //            LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //            paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo).eq(PaymentInfo::getPaymentType,paymentType);
            PaymentInfo paymentInfo = new PaymentInfo();
            //  paymentInfo.setOutTradeNo(outTradeNo);
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            paymentInfo.setPaymentType(paymentType);
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setCallbackContent(paramsMap.toString());
            //  更新数据.
            //  paymentInfoMapper.updateById(paymentInfo);
            //  paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
            updatePaymentStatus(paymentInfo,outTradeNo, PaymentType.ALIPAY.name());

            //  发送消息
            this.rabbitService.sendMsg(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());

        } catch (Exception e) {
            //  删除锁的key ，让后续的异步通知进来.
            this.redisTemplate.delete(paramsMap.get("notify_id"));
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updatePaymentStatus(PaymentInfo paymentInfo, String outTradeNo, String paymentType) {
        //  创建更新条件
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo).eq(PaymentInfo::getPaymentType,paymentType);
        //  更新数据.
        paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);

    }

    @Override
    public void paymentClose(Long orderId) {
        //  声明对象设置修改的内容
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
        //  构建条件
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderId,orderId);
        paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
    }
}
